<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <VGroup>
          <el-select class="selectClass" v-model="form.warehouseRange.type" style="max-width: 100px">
            <el-option v-for="item in warehouseOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <el-input v-model="form.warehouseRange.value" placeholder="" prefix-icon="search" clearable class="w-40" />
        </VGroup>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
        <el-select v-model="form.status" placeholder="是否启用" style="max-width: 150px">
          <el-option v-for="item in options['base.area']?.[0].areaStatus" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add"/>新增</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :options="options" @query="query"></ComModal>
  </VPage>
</template>
<script setup name="BaseArea" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { nextTick, ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryListArea } from './api'
import ComModal from './components/ComModal.vue'

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "修改时间", value: "update_time" },
]

const warehouseOptions = [
  { label: "仓库名称", value: "warehouse_name" },
  { label: "楼层名称", value: "floor_name" },
  { label: "库区名称", value: "area_name" },
  { label: "库区ID", value: "id" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      status: '',
      warehouseRange: { type: 'warehouse_name', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      field: 'id', title: '库区ID'
    },
    {
      field: 'floor_name', title: '所属楼层'
    },
    {
      field: 'area_name', title: '库区名称'
    },
    {
      field: 'status_cn', title: '是否启用'
    },
    {
      field: 'create_user', title: '创建人'
    },
    {
      field: 'create_time', title: '创建日期', width: 180
    },
    {
      field: 'update_user', title: '最近修改人'
    },
    {
      field: 'update_time', title: '修改日期', width: 180
    },
    {
      title: '操作', width: "90", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="border-right" onClick={() => open('edit', row.id)}>修改</el-link>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryListArea({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: []
})
const { options } = toRefs(data)
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "base.area"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()


const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}
</script>